package com.gongj.appuser.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;

@Configuration
@EnableAsync
public class ThreadPoolTaskConfig {

    @Autowired
    private ThreadPoolConfig threadPoolConfig;

//    @Bean("taskExecutor")
//    public Executor taskExecutor() {
//        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
//        // 核心线程数
//        executor.setCorePoolSize(threadPoolConfig.getCorePoolSize());
//        // 最大线程数
//        executor.setMaxPoolSize(threadPoolConfig.getMaxPoolSize());
//        // 空闲线程允许存活的时间
//        executor.setKeepAliveSeconds(threadPoolConfig.getKeepAliveSeconds());
//        // 等待队列大小
//        executor.setQueueCapacity(threadPoolConfig.getQueueCapacity());
//        // 设置此执行程序在关闭时应阻塞的最大秒数，以便在其余的容器继续关闭之前等待其余任务完成其执行
//        executor.setAwaitTerminationSeconds(threadPoolConfig.getAwaitTerminationSeconds());
//        // 线程名称前缀
//        executor.setThreadNamePrefix(threadPoolConfig.getThreadNamePrefix());
//        // 设置等待计划的任务在关闭时完成，不会中断正在运行的任务并执行队列中的所有任务
//        executor.setWaitForTasksToCompleteOnShutdown(threadPoolConfig.getWaitForTasksToCompleteOnShutdown());
//        // 线程池拒绝策略，如果线程池资源不足，则让调用者所在线程来执行任务
//        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
//        log.info("线程池初始化成功");
//        return executor;
//    }
}